const { app, BrowserWindow } = require('electron');
const WinState = require('electron-win-state').default;
const path = require('path');
const createTray = require('./tray')
//获取网站截图
require('./controller/getSource');
// alert
require('./controller/alert')
// openWindow
require('./controller/openWindow')
// 图片列表
require('./controller/getFiles')
// menu
require('./controller/buildMenu')
const createWindow = () => {

    //配置默认窗口大小
    const winState = new WinState({
        defaultWidth: 1000,
        defaultHeight: 800,
        electronStoreOptions: {
            name: 'window-state-main'
        }
    })
    //初始化配置
    const win = new BrowserWindow({
        ...winState.winOptions,
        webPreferences: {
            //定义预加载js
            preload: path.resolve(__dirname, "./preload/index.js")
        },
        show: false
    });


    if (app.isPackaged) {
        win.loadFile(path.join(__dirname, "../dist/index.html"));
    } else {
        const url = 'http://localhost:5173'; // 本地启动的vue项目路径
        win.loadURL(url);
    }

    const webContents = win.webContents;

    //打开开发者调试工具
    webContents.openDevTools();

    winState.manage(win);
    //页面加载完之后再打开窗口
    win.on("ready-to-show", () => {
        win.show()
    });

    createTray(app, win)
}

app.whenReady().then(() => {
    createWindow();

    //如果没有窗口打开则打开一个窗口 (macOS)
    app.on('activate', () => {
        //点击关闭按钮关闭了窗口之后 点击图标再打开
        if (BrowserWindow.getAllWindows().length === 0) {
            createWindow()
        }
    });

})

//关闭所有窗口时退出应用 (Windows & Linux)
app.on('window-all-closed', () => {
    //点击关闭按钮关闭了窗口 
    if (process.platform !== 'darwin') {
        app.quit()
    }
})